<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志清理功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #3730a3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #4f46e5;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .warning {
            border-left-color: #ffc107;
            background: #fff3cd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .cleanup-form {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }
        .stat-item {
            text-align: center;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #4f46e5;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>日志清理功能测试</h1>
    
    <div class="container">
        <h2>API连接测试</h2>
        <button onclick="testApiConnection()">测试API连接</button>
        <div id="apiResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理前统计信息</h2>
        <button onclick="getStatistics()">获取当前统计信息</button>
        <div id="statisticsResult" class="result"></div>
    </div>

    <div class="container">
        <h2>日志清理测试</h2>
        <div class="cleanup-form">
            <div class="form-group">
                <label>清理天数:</label>
                <input type="number" id="cleanupDays" value="30" min="7" max="365" placeholder="请输入清理天数">
            </div>
            <div class="form-group">
                <label>清理原因:</label>
                <input type="text" id="cleanupReason" placeholder="请输入清理原因（可选）">
            </div>
            <div class="form-group">
                <label>强制清理:</label>
                <select id="forceCleanup">
                    <option value="false">否</option>
                    <option value="true">是</option>
                </select>
            </div>
        </div>
        <button onclick="testCleanup()">执行清理</button>
        <button onclick="testCleanupPreview()">预览清理</button>
        <div id="cleanupResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理后统计信息</h2>
        <button onclick="getStatisticsAfterCleanup()">获取清理后统计信息</button>
        <div id="statisticsAfterResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5095/api';
        
        // 测试API连接
        async function testApiConnection() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '正在测试API连接...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/list?page=1&pageSize=1`);
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ API连接成功!</strong><br>
                            状态码: ${response.status}<br>
                            响应时间: ${response.headers.get('date')}<br>
                            数据: ${JSON.stringify(data, null, 2)}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ API连接失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 网络错误!</strong><br>
                        错误信息: ${error.message}<br>
                        请确保后端服务正在运行在 http://localhost:5095
                    </div>
                `;
            }
        }

        // 获取统计信息
        async function getStatistics() {
            const resultDiv = document.getElementById('statisticsResult');
            resultDiv.innerHTML = '正在获取统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 预览清理
        async function testCleanupPreview() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = `
                <div class="warning">
                    <strong>⚠️ 清理预览</strong><br>
                    清理天数: ${days} 天<br>
                    清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                    清理原因: ${reason || '未指定'}<br>
                    <strong>注意: 这只是预览，实际清理需要点击"执行清理"按钮</strong>
                </div>
            `;
        }

        // 执行清理
        async function testCleanup() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            const forceCleanup = document.getElementById('forceCleanup').value === 'true';
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            if (!confirm(`确定要清理${days}天前的日志吗？此操作不可恢复！`)) {
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = '正在执行清理...';
            
            try {
                const requestData = {
                    beforeDate: beforeDate.toISOString(),
                    reason: reason || `系统管理员清理${days}天前的过期日志`,
                    forceCleanup: forceCleanup
                };
                
                console.log('发送清理请求:', requestData);
                const response = await fetch(`${API_BASE_URL}/operationlog/cleanup`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理成功!</strong><br>
                            清理天数: ${days} 天<br>
                            清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                            清理数量: ${stats.cleanedCount} 条<br>
                            释放空间: ${(stats.freedSpaceBytes / 1024).toFixed(2)} KB<br>
                            清理原因: ${stats.reason || '未指定'}<br>
                            <details>
                                <summary>查看详细结果</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 清理失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}<br>
                            <details>
                                <summary>查看详细错误</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 清理错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 获取清理后统计信息
        async function getStatisticsAfterCleanup() {
            const resultDiv = document.getElementById('statisticsAfterResult');
            resultDiv.innerHTML = '正在获取清理后统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理后统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 页面加载时自动测试API连接
        window.onload = function() {
            testApiConnection();
        };
    </script>
</body>
</html> 
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志清理功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #3730a3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #4f46e5;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .warning {
            border-left-color: #ffc107;
            background: #fff3cd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .cleanup-form {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }
        .stat-item {
            text-align: center;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #4f46e5;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>日志清理功能测试</h1>
    
    <div class="container">
        <h2>API连接测试</h2>
        <button onclick="testApiConnection()">测试API连接</button>
        <div id="apiResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理前统计信息</h2>
        <button onclick="getStatistics()">获取当前统计信息</button>
        <div id="statisticsResult" class="result"></div>
    </div>

    <div class="container">
        <h2>日志清理测试</h2>
        <div class="cleanup-form">
            <div class="form-group">
                <label>清理天数:</label>
                <input type="number" id="cleanupDays" value="30" min="7" max="365" placeholder="请输入清理天数">
            </div>
            <div class="form-group">
                <label>清理原因:</label>
                <input type="text" id="cleanupReason" placeholder="请输入清理原因（可选）">
            </div>
            <div class="form-group">
                <label>强制清理:</label>
                <select id="forceCleanup">
                    <option value="false">否</option>
                    <option value="true">是</option>
                </select>
            </div>
        </div>
        <button onclick="testCleanup()">执行清理</button>
        <button onclick="testCleanupPreview()">预览清理</button>
        <div id="cleanupResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理后统计信息</h2>
        <button onclick="getStatisticsAfterCleanup()">获取清理后统计信息</button>
        <div id="statisticsAfterResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5095/api';
        
        // 测试API连接
        async function testApiConnection() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '正在测试API连接...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/list?page=1&pageSize=1`);
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ API连接成功!</strong><br>
                            状态码: ${response.status}<br>
                            响应时间: ${response.headers.get('date')}<br>
                            数据: ${JSON.stringify(data, null, 2)}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ API连接失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 网络错误!</strong><br>
                        错误信息: ${error.message}<br>
                        请确保后端服务正在运行在 http://localhost:5095
                    </div>
                `;
            }
        }

        // 获取统计信息
        async function getStatistics() {
            const resultDiv = document.getElementById('statisticsResult');
            resultDiv.innerHTML = '正在获取统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 预览清理
        async function testCleanupPreview() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = `
                <div class="warning">
                    <strong>⚠️ 清理预览</strong><br>
                    清理天数: ${days} 天<br>
                    清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                    清理原因: ${reason || '未指定'}<br>
                    <strong>注意: 这只是预览，实际清理需要点击"执行清理"按钮</strong>
                </div>
            `;
        }

        // 执行清理
        async function testCleanup() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            const forceCleanup = document.getElementById('forceCleanup').value === 'true';
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            if (!confirm(`确定要清理${days}天前的日志吗？此操作不可恢复！`)) {
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = '正在执行清理...';
            
            try {
                const requestData = {
                    beforeDate: beforeDate.toISOString(),
                    reason: reason || `系统管理员清理${days}天前的过期日志`,
                    forceCleanup: forceCleanup
                };
                
                console.log('发送清理请求:', requestData);
                const response = await fetch(`${API_BASE_URL}/operationlog/cleanup`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理成功!</strong><br>
                            清理天数: ${days} 天<br>
                            清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                            清理数量: ${stats.cleanedCount} 条<br>
                            释放空间: ${(stats.freedSpaceBytes / 1024).toFixed(2)} KB<br>
                            清理原因: ${stats.reason || '未指定'}<br>
                            <details>
                                <summary>查看详细结果</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 清理失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}<br>
                            <details>
                                <summary>查看详细错误</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 清理错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 获取清理后统计信息
        async function getStatisticsAfterCleanup() {
            const resultDiv = document.getElementById('statisticsAfterResult');
            resultDiv.innerHTML = '正在获取清理后统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理后统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 页面加载时自动测试API连接
        window.onload = function() {
            testApiConnection();
        };
    </script>
</body>
</html> 
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志清理功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #3730a3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #4f46e5;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .warning {
            border-left-color: #ffc107;
            background: #fff3cd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .cleanup-form {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }
        .stat-item {
            text-align: center;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #4f46e5;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>日志清理功能测试</h1>
    
    <div class="container">
        <h2>API连接测试</h2>
        <button onclick="testApiConnection()">测试API连接</button>
        <div id="apiResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理前统计信息</h2>
        <button onclick="getStatistics()">获取当前统计信息</button>
        <div id="statisticsResult" class="result"></div>
    </div>

    <div class="container">
        <h2>日志清理测试</h2>
        <div class="cleanup-form">
            <div class="form-group">
                <label>清理天数:</label>
                <input type="number" id="cleanupDays" value="30" min="7" max="365" placeholder="请输入清理天数">
            </div>
            <div class="form-group">
                <label>清理原因:</label>
                <input type="text" id="cleanupReason" placeholder="请输入清理原因（可选）">
            </div>
            <div class="form-group">
                <label>强制清理:</label>
                <select id="forceCleanup">
                    <option value="false">否</option>
                    <option value="true">是</option>
                </select>
            </div>
        </div>
        <button onclick="testCleanup()">执行清理</button>
        <button onclick="testCleanupPreview()">预览清理</button>
        <div id="cleanupResult" class="result"></div>
    </div>

    <div class="container">
        <h2>清理后统计信息</h2>
        <button onclick="getStatisticsAfterCleanup()">获取清理后统计信息</button>
        <div id="statisticsAfterResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5095/api';
        
        // 测试API连接
        async function testApiConnection() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '正在测试API连接...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/list?page=1&pageSize=1`);
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ API连接成功!</strong><br>
                            状态码: ${response.status}<br>
                            响应时间: ${response.headers.get('date')}<br>
                            数据: ${JSON.stringify(data, null, 2)}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ API连接失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 网络错误!</strong><br>
                        错误信息: ${error.message}<br>
                        请确保后端服务正在运行在 http://localhost:5095
                    </div>
                `;
            }
        }

        // 获取统计信息
        async function getStatistics() {
            const resultDiv = document.getElementById('statisticsResult');
            resultDiv.innerHTML = '正在获取统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 预览清理
        async function testCleanupPreview() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = `
                <div class="warning">
                    <strong>⚠️ 清理预览</strong><br>
                    清理天数: ${days} 天<br>
                    清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                    清理原因: ${reason || '未指定'}<br>
                    <strong>注意: 这只是预览，实际清理需要点击"执行清理"按钮</strong>
                </div>
            `;
        }

        // 执行清理
        async function testCleanup() {
            const days = parseInt(document.getElementById('cleanupDays').value);
            const reason = document.getElementById('cleanupReason').value;
            const forceCleanup = document.getElementById('forceCleanup').value === 'true';
            
            if (isNaN(days) || days < 7) {
                alert('清理天数不能少于7天');
                return;
            }
            
            if (!confirm(`确定要清理${days}天前的日志吗？此操作不可恢复！`)) {
                return;
            }
            
            const beforeDate = new Date();
            beforeDate.setDate(beforeDate.getDate() - days);
            
            const resultDiv = document.getElementById('cleanupResult');
            resultDiv.innerHTML = '正在执行清理...';
            
            try {
                const requestData = {
                    beforeDate: beforeDate.toISOString(),
                    reason: reason || `系统管理员清理${days}天前的过期日志`,
                    forceCleanup: forceCleanup
                };
                
                console.log('发送清理请求:', requestData);
                const response = await fetch(`${API_BASE_URL}/operationlog/cleanup`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理成功!</strong><br>
                            清理天数: ${days} 天<br>
                            清理日期: ${beforeDate.toLocaleDateString()} 之前的日志<br>
                            清理数量: ${stats.cleanedCount} 条<br>
                            释放空间: ${(stats.freedSpaceBytes / 1024).toFixed(2)} KB<br>
                            清理原因: ${stats.reason || '未指定'}<br>
                            <details>
                                <summary>查看详细结果</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 清理失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}<br>
                            <details>
                                <summary>查看详细错误</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 清理错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 获取清理后统计信息
        async function getStatisticsAfterCleanup() {
            const resultDiv = document.getElementById('statisticsAfterResult');
            resultDiv.innerHTML = '正在获取清理后统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 清理后统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalLogs || 0}</div>
                                    <div class="stat-label">总日志数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.todayLogs || 0}</div>
                                    <div class="stat-label">今日日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.errorLogs || 0}</div>
                                    <div class="stat-label">错误日志</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.activeUsers || 0}</div>
                                    <div class="stat-label">活跃用户</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 页面加载时自动测试API连接
        window.onload = function() {
            testApiConnection();
        };
    </script>
</body>
</html> 